package com.learn.java3y.java.annotation;

import java.beans.PropertyDescriptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class PersonTest1 {

    public static void main(String[] args) throws NoSuchFieldException {
        Field field = PersonDao1.class.getDeclaredField("person");
        try {
            Person person = (Person) field.getType().newInstance();
            Annotation annotation = field.getAnnotation(InjectPerson.class);
            Method[] methods = annotation.getClass().getMethods();
            for (Method method : methods) {
                String name = method.getName();
                try {
                    PropertyDescriptor descriptor = new PropertyDescriptor(name, Person.class);
                    Method method1 = descriptor.getWriteMethod();
                    Object o = method.invoke(annotation, null);
                    method1.invoke(person, o);
                } catch (Exception e) {
                    continue;
                }
            }
            PersonDao1 personDao = new PersonDao1();
            field.setAccessible(true);
            field.set(personDao, person);
            System.out.println(personDao.getPerson().getUsername());
            System.out.println(personDao.getPerson().getAge());
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
